//
//  UITextView+Placeholder.m
//  WeiNingProject
//
//  Created by tianjians on 2018/10/17.
//  Copyright © 2018年 卫宁数据. All rights reserved.
//

#import "UITextView+Placeholder.h"

static const void *tj_TextViewPlaceholderLableKye = "tj_TextViewPlaceholderLableKye";

static const void *tj_TextViewPlaceholderTextKey = "tj_TextViewPlaceholderTextKey";


#import <objc/runtime.h>
#import "Masonry.h"
@interface UITextView()

@property (nonatomic, strong) UILabel *placeholderLabel;

@end

@implementation UITextView (Placeholder)

-(void)setPlaceholderLabel:(UILabel *)placeholderLabel{
    objc_setAssociatedObject(self, tj_TextViewPlaceholderLableKye, placeholderLabel, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(UILabel *)placeholderLabel{
    
    UILabel *label = objc_getAssociatedObject(self, tj_TextViewPlaceholderLableKye);
    if (label == nil || ![label isKindOfClass:[UILabel class]]) {
        label = [[UILabel alloc] init];
        label.textAlignment = NSTextAlignmentLeft;
        label.backgroundColor = [UIColor clearColor];
        //        label.font = [UIFont systemFontOfSize:13];
        
        [self addSubview:label];
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            
            //make.edges.mas_equalTo(self).insets(UIEdgeInsetsMake(10, 20, 0, 0));
            make.left.mas_equalTo(8);
            make.right.mas_equalTo(-8);
            make.centerY.equalTo(self.mas_centerY);
        }];
        
        self.placeholderLabel = label;
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tj_placholderTextChange:) name:UITextViewTextDidChangeNotification object:nil];
        
    }
    return label;
}

-(void)tj_placholderTextChange:(NSNotification *)noti{
    
    if (self.text.length != 0) {
        self.placeholderLabel.text = @"";
    }else{
        self.placeholderLabel.text = self.tj_placeholder;
    }
    
}
-(UILabel *)tj_placeholderLabel{
    return self.placeholderLabel;
}

-(void)setTj_placeholder:(NSString *)tj_placeholder{
    objc_setAssociatedObject(self, tj_TextViewPlaceholderTextKey, tj_placeholder, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    if (self.text.length) {
        self.placeholderLabel.text = @"";
    }else{
        self.placeholderLabel.text = tj_placeholder;
    }
    //self.placeholderLabel.text = tj_placeholder;
    
}
-(NSString *)tj_placeholder{
    return objc_getAssociatedObject(self, tj_TextViewPlaceholderTextKey);
}


-(void)setTj_textContainerinset:(UIEdgeInsets)tj_textContainerinset{
    
    [self.placeholderLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self).insets(tj_textContainerinset);
    }];
    
    //    self.textContainerInset = tj_textContainerinset;
    
    UIEdgeInsets edge = tj_textContainerinset;
    
    edge.left = tj_textContainerinset.left - 6;
    edge.top = tj_textContainerinset.top + 2;
    
    tj_textContainerinset = edge;
    
    self.textContainerInset = tj_textContainerinset;
}

-(UIEdgeInsets)tj_textContainerinset{
    return self.textContainerInset;
}

-(void)setTj_placeholderColor:(UIColor *)tj_placeholderColor{
    self.placeholderLabel.textColor = tj_placeholderColor;
}
-(UIColor *)tj_placeholderColor{
    return self.placeholderLabel.textColor;
}
-(void)setTj_placeholderFont:(UIFont *)tj_placeholderFont{
    self.placeholderLabel.font = tj_placeholderFont;
}
-(UIFont *)tj_placeholderFont{
    return self.placeholderLabel.font;
}

@end
